
function validateFormBeforeSubmitForProjectClientMaintenance(theForm){
	var reason = "";
	
	if (theForm.projectId.value == "Select"){
		alert("Please select a project");
		theForm.clientId.value = "Select";
		document.getElementById('endDateCalender').value="";
		return false;
	}
	
	if (theForm.clientId.value == "Select"){
		alert("Please select a client");
		return false;
	}
	
	
	var clientManager = validateMe(document.getElementById('clientManager'),'Client Manager');
	clientManager = trim(clientManager);
	if (clientManager != "") {
		 alert("Errors:\n" + clientManager); 
		 return false;
	}	 
	
	var companyManager = validateMe(document.getElementById('companyManager'),'JKT Manager');
	companyManager = trim(companyManager);
	if (companyManager != "") {
		 alert("Errors:\n" + companyManager); 
		 return false;
	}	 
	
	var startDateCalender = validateMe(document.getElementById('startDateCalender'),'Start Date');
	if (startDateCalender != "") {
		 alert("Errors:\n" + startDateCalender); 
		 return false;
	}	 
	
	var clientManagerField = document.getElementById('clientManager');
    var clientManagerValue= clientManagerField.value;
    
    if(! isValidNames(clientManagerValue ,'Client Manager')){
       clientManagerField.style.background = 'Yellow'; 
       return false;
    }   
	
	var companyManagerField = document.getElementById('companyManager');
    var companyManagerValue= companyManagerField.value;
   
    if(! isValidNames(companyManagerValue ,'JKT Manager')){
       companyManagerField.style.background = 'Yellow'; 
       return false;
    }
	
	if(!validateDates()){
	return false;
	}
	
    return true;
}

function isValidNames(name , fld){

       var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
         var IsValidChar=true;
         var Char;
         for (i = 0; i < name.length && IsValidChar == true; i++)
         {
            Char = name.charAt(i);
            if(ValidChars.indexOf(Char) == -1)
            {
            alert("Please enter valid "+fld+".")
            return false;
            }
         }
         return true;

}




function validateDates(){

var startDateText = document.getElementById('startDateCalender').value;
var endDateText = document.getElementById('endDateCalender').value;

var myList= startDateText.split("/");
var myList1= endDateText.split("/");

startDateText=myList[1]+"/"+myList[0]+"/"+myList[2];
endDateText=myList1[1]+"/"+myList1[0]+"/"+myList1[2];


var startDate= new Date(startDateText)
var endDate = new Date(endDateText);

var startDateMonth = startDate.getMonth()+1;
var endDateMonth = endDate.getMonth()+1;

  
if(startDate.getFullYear()>endDate.getFullYear()){
 
alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
return false;
 
}else if(startDate.getFullYear()==endDate.getFullYear()){

if(startDateMonth>endDateMonth){
  alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
  return false;
   
 }else if(startDateMonth==endDateMonth){
  if(startDate.getDate()>endDate.getDate()){
    alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
    return false; 
   
    }else{
       return true;
     }


 }else{
    return true;
      
 }
  
}else{
 return true;
}

}


function validateMe(fld,name) { 
	var error = "";

	if (fld.value == null || (fld.value != null && fld.value.length == 0) ) {
		fld.style.background = 'Yellow'; 
		error = name+" field has not been filled in.\n"
	} else {
		var numaric = fld.value;
		for(var j=0;j < numaric.length ; j++)
		{
			if(j==0){
			var alphaa = numaric.charAt(j);
			var chCode = alphaa.charCodeAt(0);
			if(chCode == "32"){
				fld.style.background = 'Yellow'; 
				error = name+" should not start with space.\n"
			}
			}
		}
		fld.style.background = 'White';
	}
	return error;  
	}
	
	function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
	
function compareProjectClientDates(projectStartDateText,projectEndDateText,clientStartDateText,clientEndDateText){

var clientEndDateInput = clientEndDateText;

var myList= projectStartDateText.split("/");
var myList1= projectEndDateText.split("/");
var myList2 = clientStartDateText.split("/");
var myList3 = clientEndDateText.split("/");

projectStartDateText=myList[1]+"/"+myList[0]+"/"+myList[2];
projectEndDateText=myList1[1]+"/"+myList1[0]+"/"+myList1[2];
clientStartDateText =myList2[1]+"/"+myList2[0]+"/"+myList2[2];
clientEndDateText =myList3[1]+"/"+myList3[0]+"/"+myList3[2];

formattedClientStartDateText = myList2[0]+"/"+myList2[1]+"/"+myList2[2];
formattedClientEndDateText = myList3[0]+"/"+myList3[1]+"/"+myList3[2];

var projectStartDate= new Date(projectStartDateText);
var projectEndDate = new Date(projectEndDateText);
var clientStartDate = new Date(clientStartDateText);
var clientEndDate = new Date(clientEndDateText);

var projectStartDateMonth = projectStartDate.getMonth()+1;
var projectEndDateMonth = projectEndDate.getMonth()+1;
var clientStartDateMonth = clientStartDate.getMonth()+1;
var clientEndDateMonth = clientEndDate.getMonth()+1;
  
if(clientStartDate.getFullYear()>projectStartDate.getFullYear()){
if(formattedClientEndDateText=="/undefined/undefined"){
alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates."); 
}else{
alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates."); 
}
return false;
}else if(clientStartDate.getFullYear()==projectStartDate.getFullYear()){

   if(clientStartDateMonth>projectStartDateMonth){
	if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
	   
   }else if(clientStartDateMonth==projectStartDateMonth){
    if(clientStartDate.getDate()>projectStartDate.getDate()){
   if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
    }
   }
 }

if(projectStartDate.getFullYear()>clientEndDate.getFullYear()){
 
	if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
 
}else if(projectStartDate.getFullYear()==clientEndDate.getFullYear()){

   if(projectStartDateMonth>clientEndDateMonth){
   if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
   }else if(projectStartDateMonth==clientEndDateMonth){
    if(projectStartDate.getDate()>clientEndDate.getDate()){
    if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
    }

   }
  
}

if(projectEndDate.getFullYear()>clientEndDate.getFullYear()){
 
	if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
 
}else if(projectEndDate.getFullYear()==clientEndDate.getFullYear()){

   if(projectEndDateMonth>clientEndDateMonth){
   if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
   }else if(projectEndDateMonth==clientEndDateMonth){
    if(projectEndDate.getDate()>clientEndDate.getDate()){
    if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false; 
   
    }

   }
  
}


if(clientEndDate.getFullYear()<projectStartDate.getFullYear()){
 
	if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
 
}else if(clientEndDate.getFullYear()==projectStartDate.getFullYear()){

   if(clientEndDateMonth<projectStartDateMonth){
   if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
   }else if(clientEndDateMonth==projectStartDateMonth){
    if(clientEndDate.getDate()<projectStartDate.getDate()){
   if(formattedClientEndDateText=="/undefined/undefined"){
	alert("Errors:\n"+"project start date and end date should be later or equal to client start date("+formattedClientStartDateText+").\nGo to Project Maintenance screen to change the Project dates.");  
	}else{
	alert("Errors:\n"+"Project start date and end date should be in between the client date range("+formattedClientStartDateText+" - "+formattedClientEndDateText+").\nGo to Project Maintenance screen to change the Project dates.");
	}
	return false;
   
    }

   }
  
}

return true;

}
	
	